NameSpaces
Namespaces are important because there has been an explosion of variable, method, property, and class names over the past few years. These include library routines, third-party code, and your own code. Without namespaces, all of these names would compete for slots in the global namespace and conflicts would arise. For example, if your program defined a class called vision, it could conflict with another class called Vision supplied by a third-party library that your program uses. Fortunately, namespaces prevent this type of problem, because a namespace localizes the visibility of names declared within it. A namespace is declared using the namespace keyword.
names declared in one namespace will not conflict with the same names declared in another. The namespace used by the .NET Framework library (which is the C# library) is System. This is why you have included
using System;
namespace name {
// members
}
Anything defined within a namespace is within the scope of that namespace. Thus, namespace defines a scope. Within a namespace you can declare classes, structures, delegates, enumerations, interfaces, or another namespace.
Program on Namespace
namespace vision
{
class sample
{
public static void Main()
{
System.Console.WriteLine("Welcome to Name spaces");
}
}
}
program to find the sum of 2nos using namespaces
using System;
namespace vision
{
class sum
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum of 2nos{0}", (a + b));
}
}
}
class sample
{
public static void Main()
{
vision.sum x = new vision.sum();
x.get(10, 20);
x.put();
}
}
program on multiple namespaces with same class and methods
using System;
namespace vision1
{
class abc
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum{0}", (a + b));
}
}
}
namespace vision2
{
class abc
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum{0}", (a + b));
}
}
}
namespace vision3
{
class sample
{
public static void Main()
{
vision1.abc x = new vision1.abc();
vision2.abc y = new vision2.abc();
Console.WriteLine("Vision1 namespace abc class executed");
x.get(4, 5);
x.put();
Console.WriteLine("Vision2 namespace abc class executed");
y.get(3, 5);
y.put();
}
}
}
Alias name to the namespace:
using alias = name;
alias becomes another name for the class or namespace specified by name.
Program on Aliasing namespace
using System;
namespace vision1
{
class abc
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum{0}", (a + b));
}
}
}
namespace vision2
{
class abc
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum{0}", (a + b));
}
}
}
namespace vision3
{
using vis = vision1.abc;
using vis1 = vision2.abc;
class sample
{
public static void Main()
{
vis x = new vis();
vis1 y = new vis1();
Console.WriteLine("Vision1 namespace abc class executed");
x.get(4, 5);
x.put();
Console.WriteLine("Vision2 namespace abc class executed");
y.get(3, 5);
y.put();
}
}
}
using
using is a keyword to bring namespaces that you created into view . All of the members defined within the specified namespace are brought into view. A using directive must be specified at the top of each file, prior to any other declarations.
Syn: using name;
Program on using created namespace
using System;
using vision1;
namespace vision1
{
class abc
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum{0}", (a + b));
}
}
}
namespace vision
{
class sample
{
public static void Main()
{
abc x = new abc();
x.get(4, 5);
x.put();
}
}
}
We can write more than one namespace with the samename in the program at that time the class names suouldbe distinct.
Program on recurent namespaces
using System;
using vision;
namespace vision
{
class abc
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum{0}", (a + b));
}
}
}
namespace vision
{
class xyz
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Subtraction{0}", (a - b));
}
}
}
namespace vision
{
class sample
{
public static void Main()
{
abc x = new abc();
xyz y = new xyz();
x.get(4, 2);
x.put();
y.get(22, 2);
y.put();
}
}
}
Program on nested Namespaces
using System;
namespace vision
{
class abc
{
int a, b;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Triangle{0}", (a * b)/2);
}
}
namespace visi
{
class xyz
{
int r;
public void get(int x)
{
r = x;
}
public void put()
{
Console.WriteLine("Area of Circle{0}", (22 / 7 * r * r));
}
}
}
}
namespace vision1
{
class sample
{
public static void Main()
{
vision.abc x = new vision.abc();
vision.visi.xyz y = new vision.visi.xyz();
x.get(5, 3);
x.put();
y.get(3);
y.put();
}
}
}
:: Alias Qulifier:
When the same name is declared within two different namespaces and you then try to bring both namespaces into view by using statement it will conflict the classes. In that situation we can use :: namespace alias qualifier to explicitly specify which namespace is intended.
Program on :: qulifier
using System;
using vision;
using vision1;
using my = vision;
namespace vision
{
class abc
{
public void put()
{
Console.WriteLine("This is Vision name space put method");
}
}
}
namespace vision1
{
class abc
{
public void put()
{
Console.WriteLine("This is vision1 name space put method");
}
}
}
class sample
{
public static void Main()
{
my::abc x = new my::abc();
vision1 .abc y=new vision1.abc ();
x.put();
y.put();
}
}
Program on Global keyword
using System;
using my = vision;
namespace vision
{
class abc
{
public void put()
{
Console.WriteLine("This is Vision name space put method");
}
}
}
class abc
{
public void put()
{
Console.WriteLine("This is vision1 name space put method");
}
}
class sample
{
public static void Main()
{
my::abc x = new my::abc();
global::abc y = new global::abc();
x.put();
y.put();
}
}